package com.agree;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.android.ddmlib.AdbCommandRejectedException;
import com.android.ddmlib.CollectingOutputReceiver;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.IDevice.DeviceUnixSocketNamespace;
import com.android.ddmlib.ShellCommandUnresponsiveException;
import com.android.ddmlib.TimeoutException;

public class Main {

	private String MINITOUCH_BIN = "minitouch";
	private String MINITOUCH_START_COMMAND = "/data/local/tmp/%s";
	
	public static void main(String[] args) {
		 ADB adb = new ADB();
		 IDevice  device = adb.getDevices()[0];
		try {
			new Main(device).startSth();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private IDevice device;
	
	public Main(IDevice device) {
		this.device = device;
	}
	
	private String REMOTE_PATH = "/data/local/tmp";
	private String MINICAP_CHMOD_COMMAND = "chmod 777 %s/%s";
	public void startSth()throws Exception {
		
//		device.pushFile("D:\\05-mworkspace\\21-powerjob\\show\\lib\\minitouch", REMOTE_PATH
//				+ File.separator + MINITOUCH_BIN);
		
		executeShellCommand(String.format(MINICAP_CHMOD_COMMAND,
				REMOTE_PATH, MINITOUCH_BIN));
		
		
		String startCmd = String.format(MINITOUCH_START_COMMAND,MINITOUCH_BIN);
		
		device.createForward(1111, "minitouch",
				DeviceUnixSocketNamespace.ABSTRACT);
		
		System.out.println(startCmd);
		String str = executeShellCommand(startCmd);
		System.out.println(str);
	}
	
	private String executeShellCommand(String command) {
		CollectingOutputReceiver output = new CollectingOutputReceiver();
		try {
			device.executeShellCommand(command, output,5,TimeUnit.SECONDS);
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AdbCommandRejectedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ShellCommandUnresponsiveException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return output.getOutput();
	}
}
